package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * redis 的持久化  通过spring的定时器 , 进行操作
 */
//@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Scheduled(cron = "0/5 * * * * ?")  //每个五秒 操作一次  定时器
    public void doword() {
        //持久化的数据 是统计数据
        System.out.println("----vo持久化开始----");
        //1.从redis 获取所有vo对象 通过key前缀 ,从redis中获取指定的对象
        List<StrategyStatisVO> list = strategyStatisVOService.getVOsByPattern(RedisKeys.STRATEGY_STATIS_VO.join("*"));
        //2.将vo对象 update进mysql数据库
        StrategyStatisPersistenceVO pvo = null;
        for (StrategyStatisVO vo : list) {
            pvo = new StrategyStatisPersistenceVO();
            BeanUtil.copyProperties(pvo, vo);
            strategyDetailService.updateStatisVO(pvo);
        }
        System.out.println("----vo持久化结束----");
    }
}
